//
//  WelcomeViewController.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/16.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    //更换view
    
    override func loadView() {
        super.loadView()
        
        view = backView
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()

       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //MARK:设置UI
    
    private func setUpUI(){
    
        view.addSubview(iconView)
        
        view.addSubview(welcomeLabel)
        
        //MARK:设置约束
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-200)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
    
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(15)
        }
        
        
        
    iconView.sd_setImageWithURL((UserAccountViewModel().iconImageURL ?? "") as! NSURL, placeholderImage: UIImage(named: "avatar_default_big"))
 
        //把头像切成圆形
        
        iconView.layer.cornerRadius = 50
        iconView.layer.masksToBounds = true
        
        
        startAnimation()
        
    
    }
    
    //MARK:加动画
    
    func startAnimation(){
        
        welcomeLabel.alpha = 0
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-ScreenHeight + 200)
        }

    
    
     UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
        
        self.view.layoutIfNeeded()
        
        
     }) { (_) -> Void in
        
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.welcomeLabel.alpha = 1.0
        }, completion: { (_) -> Void in
            //在动画完成之后发送通知,跳转到首页
         NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
            
            
        })
        
        }
    
    
    }
    

    
    //MARK:懒加载视图
    
    lazy var backView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var iconView:UIImageView = UIImageView()
    
    lazy var welcomeLabel:UILabel = {
        
        let label = UILabel()
        
        label.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
        
        label.textColor = UIColor.lightGrayColor()
        
        
         label.sizeToFit()
    
        return label
        }()
    
    
    
    


}
